package come.bite.spring.demo;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/resp")
//@RestController      //  @RestController = @ResponseBody + @Controller
@Controller            //@ResponseBody(返回数据)  @Controller(返回页面)  @RestController(返回数据和页面)
public class RespController {
    @RequestMapping("/r1")
    public String returnPage() {
        return "/index.html";
    }

    @ResponseBody //表示返回数据
    @RequestMapping("/r2")     //一个类既有返回页面的方法也有返回数据的方法 在返回数据的方法上加@ResponseBody
    public String returnDate() {
        return "这是前端返回的数据";
    }

    @ResponseBody
    @RequestMapping("/r3")   //返回标签片段
    public  String returnHTML(){
        return "<h1>我是一个html标签</h1>";
    }

    @ResponseBody
    @RequestMapping(value = "/r4" ,produces = "text/plain")   //将html改为文本类型
    public  String returnTest(){
        return "<h1>我是一个html标签</h1>";
    }

    @ResponseBody
    @RequestMapping(value = "/r5" )   //返回Json
    public  user returnJson(){
        user user = new user("zhangsan",10,"123");
        return user;
    }

    @ResponseBody
    @RequestMapping(value = "/r6" )   //设置状态码
    public  user setStatue(HttpServletResponse response){
        response.setStatus(401);  //设置状态码
        user user = new user("zhangsan",10,"123");
        return user;
    }

    @ResponseBody
    @RequestMapping(value = "/r7" )   //设置Header
    public String setHeader(HttpServletResponse response){
        response.setHeader("myHeader","myHeader");  //设置Header
        return "设置Header成功";
    }
}
